﻿<cfcomponent output="false">

<cffunction name="init" access="public" output="false" hint="I am the object constructor">
	<cfargument name="dsn">
	<cfargument name="username" default="">
	<cfargument name="password" default="">
	
	<cfscript>
	var platform = "";
	var obj = this;
	
	VARIABLES.dsn = ARGUMENTS.dsn;
	VARIABLES.username = ARGUMENTS.username;
	VARIABLES.password = ARGUMENTS.password;
	
	platform = getPlatform();
	
	obj =  createObject("component","genesis_#platform#").init(argumentCollection:ARGUMENTS);
	
	return obj;
	</cfscript>
</cffunction>

<cffunction name="getTables" access="public" returntype="string" output="false" hint="I get a list of all tables in the current database.">

	<cfargument name="dbname">
	
	<cfset var rsTables = "">
	<cfset var tables = "">
	
	<cfDBInfo name="rsTables"
			type="tables" dbname="#ARGUMENTS.dbname#"
			datasource="#VARIABLES.dsn#" />
			
	<cfset tables = valueList(rsTables.table_name)>
	
	<cfreturn tables>
</cffunction>

<cffunction name="getTableStruct" access="public" returntype="struct" output="false" hint="I get a structure of the requested table in the current database.">

	<cfargument name="table">
	
	<cfset var strTable = "">
	
	<cfDBInfo name="strTable"
		type="columns" table="#ARGUMENTS.table#"
		datasource="#VARIABLES.dsn#" />
	
	<cfreturn strTable>
</cffunction>

<cffunction name="getPlatform" access="public" returntype="string" output="no" hint="I return the database platform being used.">
	
	<cfset var connection = 0>
	<cfset var db = "">
	<cfset var type = "">
	<cfset var qDatabases = getSupportedPlatforms()>
	
	<cfif Len(variables.dsn)>
		<cfset connection = getConnection()>
		<cfset db = connection.getMetaData().getDatabaseProductName()>
		<cfset connection.close()>
		
		<cfswitch expression="#db#">
		<cfcase value="Microsoft SQL Server">
			<cfset type = "MSSQL">
		</cfcase>
		<cfcase value="MySQL">
			<cfset type = "MYSQL">
		</cfcase>
		<cfcase value="PostgreSQL">
			<cfset type = "PostGreSQL">
		</cfcase>
		<cfcase value="Oracle">
			<cfset type = "Oracle">
		</cfcase>
		<cfcase value="MS Jet">
			<cfset type = "Access">
		</cfcase>
		<cfcase value="Apache Derby">
			<cfset type = "Derby">
		</cfcase>
		<cfdefaultcase>
			<cfif ListFirst(db,"/") EQ "DB2">
				<cfset type = "DB2">
			<cfelse>
				<cfset type = "unknown">
				<cfset type = db>
			</cfif>
		</cfdefaultcase>
		</cfswitch>
	<cfelse>
		<cfset type = "Sim">
	</cfif>
	
	<cfreturn type>
</cffunction>

<cffunction name="getSupportedPlatforms" access="public" returntype="query" output="no" hint="I return the databases supported by this installation of DataMgr.">
	
	<cfset var qComponents = 0>
	<cfset var aComps = ArrayNew(1)>
	<cfset var i = 0>
	<cfset var qDatabases = QueryNew("Database,DatabaseName,shortstring,driver")>
	
	<cfif StructKeyExists(variables,"databases") AND isQuery(variables.databases)>
		<cfset qDatabases = variables.databases>
	<cfelse>
		<cfdirectory action="LIST" directory="#GetDirectoryFromPath(GetCurrentTemplatePath())#" name="qComponents" filter="*.cfc">
		<cfloop query="qComponents">
			<cfif name CONTAINS "genesis_">
				<cftry>
					<cfset ArrayAppend(aComps,CreateObject("component","#ListFirst(name,'.')#").init(""))>
					<cfset QueryAddRow(qDatabases)>
					<cfset QuerySetCell(qDatabases, "Database", ReplaceNoCase(ListFirst(name,"."),"DataMgr_","") )>
					<cfset QuerySetCell(qDatabases, "DatabaseName", aComps[ArrayLen(aComps)].getDatabase() )>
					<cfset QuerySetCell(qDatabases, "shortstring", aComps[ArrayLen(aComps)].getDatabaseShortString() )>
					<cfset QuerySetCell(qDatabases, "driver", aComps[ArrayLen(aComps)].getDatabaseDriver() )>
					<cfcatch>
					</cfcatch>
				</cftry>
			</cfif>
		</cfloop>
		<cfset variables.databases = qDatabases>
	</cfif>

	<cfreturn qDatabases>
</cffunction>

<cffunction name="getConnection" access="private" returntype="any" output="false" hint="Returns a java.sql.Connection (taken from Transfer with permission).">
	<cfscript>
	var datasourceService = createObject("Java", "coldfusion.server.ServiceFactory").getDataSourceService();

	if( StructKeyExists(variables,"username") AND StructKeyExists(variables,"password") ) {
		return datasourceService.getDatasource(variables.dsn).getConnection(variables.username, variables.password);
	} else {
		return datasourceService.getDatasource(variables.dsn).getConnection();
	}
	</cfscript>
</cffunction>

<cffunction name="getTypeOfCFType" access="private" returntype="any" output="false" hint="">
	<cfargument name="CF_DataType" type="string" required="yes">
	
	<cfset var result = "">
	
	<cfswitch expression="#arguments.CF_DataType#">
	<cfcase value="CF_SQL_BIT">
		<cfset result = "boolean">
	</cfcase>
	<cfcase value="CF_SQL_DECIMAL,CF_SQL_DOUBLE,CF_SQL_FLOAT,CF_SQL_NUMERIC">
		<cfset result = "numeric">
	</cfcase>
	<cfcase value="CF_SQL_BIGINT,CF_SQL_INTEGER,CF_SQL_SMALLINT,CF_SQL_TINYINT">
		<cfset result = "integer">
	</cfcase>
	<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
		<cfset result = "date">
	</cfcase>
	</cfswitch>
	
	<cfreturn result>
</cffunction>

</cfcomponent>